/**
 * Sample classes extending the core UI Package.
 * @author Martin Kleppe <kleppe@ubilabs.net>
 * @see http://code.google.com/p/jiggytype/
 */

Plugins.load("UIKit");
include("scripts/prototype.js");
var ui = {
	Window: Class.create({
		initialize: function(options){
			this.options = {
				hidden: false,
				front: true,
				key: true,
				background: "#FFFF00"
			};
			Object.extend(this.options, options || {});
			this._native = new UIWindow(
				UIHardware.fullScreenApplicationContentRect
			);
			this._native.setHidden(this.options.hidden);
			(this.options.front && this._native.orderFront());
			(this.options.key && this._native.makeKey());
			if (this.options.background){
				//TODO
				this._native.backgroundColor = [1, 1, 1, 1];
			}
			this.bounds = this._native.bounds;
			this.width = this.bounds[2];
			this.height = this.bounds[3];
		}, 
		
		set: function(view){
			view = view._native || view;
			this._native.setContentView(view);
			return this;
		}
	}),
	
	Scroller: Class.create({
		initialize: function(options){
			this.options = Object.extend({
				width: 100,
				height: 100
			}, options || {});
			this._native = new UIScroller();
			this._native.contentSize = [
				this.options.width, 
				this.options.height
			];
		}, add: function(){
			$A(arguments).each(function(view){
				view = view._native || view;
				this._native.addSubview(view);
			}.bind(this));
			return this;
		}
	}),
	
	TextLabel: Class.create({
		initialize: function(options){
			this.options = Object.extend({
				x: 0,
				y: 0,
				width: 320,
				height: 240,
				wrap: false,
				center: false,
				font: "",
				fontsize: 16,
				text: ""
			}, options || {});
		
			this._native = new UITextLabel([
				this.options.x, this.options.y, 
				this.options.width, this.options.height
			]); 
			
			if (this.options.text){
				this.update(this.options.text);
			}
			
			if (this.options.font){
				this._native.setFont(
					new Font(this.options.font, 0, this.options.fontsize)
				);
			}
			
			if (this.options.wrap){
				this._native.wrapsText = true;
			}
			
			if (this.options.center){
				this.centersHorizontally = true;				
			}
		},
		
		getText: function(){
			return this._native.text;
		},
		
		update: function(text){
			this._native.text = text;
			return this;
		},
		
		add: function(text){
			this._native.text += text + "\n";
			return this;
		}
	}),
	
	ImageView: Class.create({
		initialize: function(url, options){
			this._native = new UIImageView(
				new Image(url)
			);
		}
	})
};
